package com.laboratory.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laboratory.mapper.LoginLogMapper;
import com.laboratory.model.entity.LoginLog;
import com.laboratory.model.vo.loginLog.LoginLogEeVo;
import com.laboratory.model.dto.loginLog.LoginLogQueryDto;
import com.laboratory.service.LoginLogService;
import org.springframework.stereotype.Service;

/**
 * @author lucky
 * @description 针对表【login_log(登录日志)】的数据库操作Service实现
 * @createDate 2024-04-02 20:10:54
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog>
        implements LoginLogService {

    @Override
    public PageInfo<LoginLog> getAll(Integer pIndex, Integer pSize, LoginLogQueryDto loginLog) {
        // 开启分页
        PageHelper.startPage(pIndex, pSize);

        // 构造条件
        QueryWrapper<LoginLog> qw = new QueryWrapper<>();

        // 判断对象是否为空
        if (ObjectUtil.isNotEmpty(loginLog)) {

            // 用户名
            if (StrUtil.isNotEmpty(loginLog.getUserName())) {
                qw.like("user_name", loginLog.getUserName());
            }

            // 登录地址
            if (StrUtil.isNotEmpty(loginLog.getLoginAddr())) {
                qw.like("login_addr", loginLog.getLoginAddr());
            }

            // 登录状态
            if (ObjectUtil.isNotEmpty(loginLog.getLoginState())) {
                qw.eq("login_state", loginLog.getLoginState());
            }
        }

        //根据id倒序
        qw.orderByDesc("id");

        return new PageInfo<>(baseMapper.selectList((qw)));
    }

    @Override
    public void importLoginLog() {
        EasyExcel.write("D:\\登录日志.xlsx", LoginLogEeVo.class).sheet("登录日志").doWrite(baseMapper.selectList(null));
    }
}




